/*
 * @Author: your name
 * @Date: 2021-05-11 09:56:11
 * @LastEditTime: 2021-05-13 14:11:05
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vite-project/mock/user.js
 */
const _ = require('./utils/index');

const mock = {};

function createFakeUserList() {
    return [
        {
            userId: 'admin',
            login_name: 'admin',
            realName: 'HR Admin',
            desc: 'manager',
            login_password: '1',
            token: 'admin',
            roles: [
                {
                    roleName: 'Super Admin',
                    value: 'super',
                },
            ],
        },
        {
            userId: 'user',
            login_name: 'test',
            login_password: '123456',
            realName: 'test user',
            desc: 'tester',
            token: 'user',
            roles: [
                {
                    roleName: 'Tester',
                    value: 'test',
                },
            ],
        },
    ];
}

mock[`POST /user/login`] = async (req, res) => {
    const body = await _.getPostData(req, res);
    const { login_name, login_password } = body;

    const checkUser = createFakeUserList().find(
        (item) =>
            item.login_name === login_name &&
            login_password === item.login_password
    );
    if (!checkUser) {
        return res.send(_.resultError())
    }
    const {
        token,
    } = checkUser;
    return res.send(_.resultSuccess(checkUser, token, "登录成功！"));
};

mock[`POST /user/getUserInfoById`] = async (req, res) => {
    const body = await _.getPostData(req, res);
    const { userId } = body;
    const checkUser = createFakeUserList().find(
        (item) => item.userId === userId
    );
    if (!checkUser) {
        return res.send(_.resultError());
    }
    return res.send(_.resultSuccess({...checkUser},"admin"));
};

module.exports = {
    ...mock,
};
